package cc.vv.lkbasecomponent.base.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import cc.vv.lkbasecomponent.util.LkNetworkUtil;

/**
 * 网络状态改变服务
 *
 * @author dupeng dupeng@vv.cc
 * @date 2018/3/21
 */

public class ConnectChangeService extends Service {
    /**
     * 网络状态改变广播Action
     */
    public static String NET_CHANGE_ACTION = "NET_CHANGE_ACTION";
    /**
     * 状态key
     */
    public static final String TYPE_KEY = "TYPE_KEY";
    /**
     * 网络断开
     */
    public static int TYPE_NONE = -1;
    /**
     * 移动网络
     */
    public static int TYPE_MOBILE = 1;
    /**
     * WIFI网络
     */
    public static int TYPE_WIFI = 2;

    /**
     * 网络监听器
     */
    private ConnectionChangeReceiver connectionChangeReceiver;

    private LocalBinder localBinder = new LocalBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return localBinder;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        connectionChangeReceiver = new ConnectionChangeReceiver();
        registerReceiver(connectionChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }


    private class ConnectionChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent intentDis = new Intent(NET_CHANGE_ACTION);

            boolean isConnected = true;
            if (!LkNetworkUtil.isConnected()) {
                //网络断开
                isConnected = false;
            } else {
                int networkType = LkNetworkUtil.getNetworkType(context);
                switch (networkType) {
                    case LkNetworkUtil.NETTYPE_WIFI:
                        //wifi
                        intentDis.putExtra(TYPE_KEY, TYPE_WIFI);
                        sendBroadcast(intentDis);
                        break;
                    case LkNetworkUtil.NETTYPE_CMNET:
                    case LkNetworkUtil.NETTYPE_CMWAP:
                        //移动流量
                        intentDis.putExtra(TYPE_KEY, TYPE_MOBILE);
                        sendBroadcast(intentDis);
                        break;
                    default:
                        //无网络
                        isConnected = false;
                        break;
                }
            }

            if (!isConnected) {
                //断网，发送没网的通知
                intentDis.putExtra(TYPE_KEY, TYPE_NONE);
                sendBroadcast(intentDis);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (connectionChangeReceiver != null) {
            unregisterReceiver(connectionChangeReceiver);
        }
    }


    public class LocalBinder extends Binder {
        ConnectChangeService getService() {
            return ConnectChangeService.this;
        }
    }
}
